package com.egao.common.module.cms.controller;

import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.cms.entity.Official;
import com.egao.common.module.cms.service.OfficialService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 信息控制层
 * @author pyx
 */
@Api(tags = "信息管理")
@RestController
@RequestMapping("${api}")
public class OfficialController extends BaseController {
    @Autowired
    private OfficialService officialService;

    @PreAuthorize("hasAuthority('cms:official:list')")
    @ApiOperation("分页查询信息")
    @OperLog(value = "信息管理", desc = "分页查询信息")
    @GetMapping("/cms/official/page")
    public PageResult<Official> page(HttpServletRequest request) {
        PageParam<Official> pageParam = new PageParam<>(request);
        pageParam.setDefaultOrder(null, new String[]{"create_time"});
        return new PageResult<>(officialService.listPage(pageParam),pageParam.getTotal());
    }

    @PreAuthorize("hasAuthority('cms:official:insert')")
    @ApiOperation("添加信息")
    @OperLog(value = "信息管理", desc = "添加信息", result = true)
    @PostMapping("/cms/official/insert")
    public JsonResult insert(@RequestBody Official official) {
        return officialService.insertOfficial(official);
    }

    @PreAuthorize("hasAuthority('cms:official:update')")
    @ApiOperation("修改信息")
    @OperLog(value = "信息管理", desc = "修改信息", result = true)
    @PutMapping("/cms/official/update")
    public JsonResult update(@RequestBody Official official) {
        return officialService.updateOfficial(official);
    }

    @PreAuthorize("hasAuthority('cms:official:delete')")
    @ApiOperation("删除信息")
    @OperLog(value = "信息管理", desc = "删除信息", result = true)
    @DeleteMapping("/cms/official/delete")
    public JsonResult delete(Integer id) {
        return officialService.deleteOfficial(id);
    }


    @PreAuthorize("hasAuthority('cms:official:detail')")
    @ApiOperation("查询单个信息")
    @OperLog(value = "信息管理", desc = "查询单个信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "信息id", required = false, dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/cms/official/get")
    public JsonResult listGetId(Integer id) {
        return officialService.getId(id);
    }
}
